// pages/shop/login.js
var app = getApp();
var phone;

Page({
  onReady: function(){
    var that = this;
    app.util.appInfo(app, function(res){
      var shopLoginLogo = '/images/food.png';
      if(res.result.shop_login_logo.length > 0){
        shopLoginLogo = res.result.shop_login_logo;
      }
      that.setData({
        shop_login_logo: shopLoginLogo
      });
    });
  },
  onLoad:function(options){
    var token = wx.getStorageSync('token');
    if (!token) {
      app.util.newLogin(app, function (res) {
      // if(res){
      //   //如果商户已登录
      //   var shop = wx.getStorageSync('shop');
      // }
      });
    }else{
      //如果商户已登录
      var shop = wx.getStorageSync('shop');
      if (shop.length != 0) {
        app.func.req('index/business_login', {
          phone: shop.phone,
          app_id: shop.app_id,
        }, 'POST', function(res){ 
          if (res.code == 'success') {
            wx.setStorageSync('shop', res.result);
            wx.switchTab({
              url: './products'
            });
          }
        },{
          'token': wx.getStorageSync('token')
        });
      }
    }
  },
  businessLogin: function(e){
    if(!wx.canIUse('button.open-type.getUserInfo')){
      wx.showModal({
        title: '提示',
        content: '请升级手机微信版本6.5.10及以上',
        showCancel: false
      })
      return;
    }

    if (e.detail.value) {
      phone = e.detail.value.phone;
    }

    if (phone.trim().length == 0) {
      wx.showToast({
        title: '请授权手机号',
        image: '/images/fail.png',
        duration: 3000
      });
      return;
    }

    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.util.userAuth(app, e.detail, function(res){
        app.func.req('index/business_login', {
          phone: phone,
          app_id: app.app_id
        }, 'POST', function(res){
          console.log(res)
          var errMsg = '登录失败';
          if (res.code == 'success') {
            wx.setStorageSync('shop', res.result);
            app.refreshProducts = true;

            wx.switchTab({
              url: './products'
            });
            return;
          } else if (res.msg == 'business.login.code.expire') {
            errMsg = '验证码已失效';
          } else if (res.msg == 'shops.not.exists') {
            errMsg = '店铺不存在';
          } else if (res.msg == 'shops.status.deny') {
            errMsg = '店铺已关闭';
          } else if (res.msg == 'business.has.bind') {
            errMsg = '店铺已被绑定';
          } else if (res.msg == 'business.login.code.error') {
            errMsg = '验证码错误';
          } else if (res.msg == 'phone.try.too.little') {
            errMsg = '验证码太频繁';
          } else {
            errMsg = '登录失败';
          }

          wx.showToast({
            title: errMsg,
            image: '/images/fail.png',
            duration: 3000
          });
        },{
          'token': wx.getStorageSync('token')
        });
      })
    }
  },
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e.detail.errMsg)
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      app.func.req("users/auth_phone", {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }, "POST", function (res) {
        if (res.code == 'success') {
          that.setData({
            phone: res.result
          });
        }
      }, {
          'token': wx.getStorageSync('token')
        });
    }
  }
})
